package com.tledu.cyrus.web;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * @author cyrus
 */
@WebServlet("/file")
public class ResponseFileServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("application/octet-stream; charset=utf-8");
        resp.setHeader("Content-Disposition","attachment; filename=\"xihaiqingge.kgm\"");
        OutputStream os = resp.getOutputStream();
        InputStream is = new FileInputStream("E:\\02授课留档\\java34期\\java34期课堂案例\\java34\\profile\\刀郎 - 西海情歌.kgm");
        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = is.read(buffer)) != -1) {
            os.write(buffer, 0, len);
        }
    }
}
